package com.ke.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ke.dao.ArticleOrderRepository;
import com.ke.dao.ArticleRepository;
import com.ke.pojo.Article;
import com.ke.pojo.ArticleCart;
import com.ke.pojo.ArticleOrder;
import com.ke.pojo.BaseResp;
import com.ke.service.ArticleOrderService;
import com.ke.utils.ArticlePayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.persistence.Id;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class ArticleOrderServiceImpl implements ArticleOrderService {

    @Autowired
    RedisTemplate redisTemplate;


    @Autowired
    ArticleOrderRepository articleOrderRepository;

    @Override
    public String addArticleOrder(List<Integer> ids, HttpServletRequest request) {
        //获取用户id
        Integer uid= new Integer(request.getAttribute("id").toString());
        Double price = 0.0;
        Boolean flag = redisTemplate.hasKey("articleCart"+uid.toString());
        UUID uuid = UUID.randomUUID();
        if(flag){
            for(Integer id :ids){
                //从redis购物车中获取文章信息
                Object o = redisTemplate.opsForHash().get("articleCart" + uid.toString(), id.toString());
                String s = JSONObject.toJSONString(o);
                ArticleCart articleCart = JSONObject.parseObject(s, ArticleCart.class);
                price+=articleCart.getPrice();
                ArticleOrder articleOrder = new ArticleOrder();
                articleOrder.setArticleId(id);
                articleOrder.setCreateTime(new Date());
                articleOrder.setOrderNum(uuid.toString());
                articleOrder.setUserId(uid);
                articleOrder.setStatus("WAIT_PAY");
                articleOrder.setPrice(articleCart.getPrice());
                articleOrder.setArticleName(articleCart.getArticleName());
                articleOrderRepository.save(articleOrder);
                redisTemplate.opsForHash().delete("articleCart" + uid.toString(), id.toString());
            }
            Map map = new HashMap();
            map.put("orderId",uuid.toString());
            map.put("price",price);
            return ArticlePayUtil.pay(map);
        }else{
            return "支付失败";
        }
    }

    /**
     * 修改订单状态
     * @param orderId
     * @param trade_status
     */
    @Override
    public void updateOrderStatus(String orderId, String trade_status) {
        //通过订单编号查询出文章列表
        List<ArticleOrder> byOrderNum = articleOrderRepository.findByOrderNum(orderId);
        for(ArticleOrder articleOrder : byOrderNum){
            articleOrder.setStatus(trade_status);
            articleOrderRepository.saveAndFlush(articleOrder);
        }
    }

    /**
     * 查询全部订单
     * @param request
     * @return
     */
    @Override
    public BaseResp findAllArticleOrder(HttpServletRequest request) {
        List<ArticleOrder> list = articleOrderRepository.findAll();
        return new BaseResp(0,"查询成功",list);
    }

    /**
     * 删除订单
     * @param request
     * @return
     */
    @Override
    public BaseResp deleteArticleOrder(Map map,HttpServletRequest request) {
        Integer id = Integer.valueOf(map.get("id").toString());
        articleOrderRepository.deleteById(id);
        return new BaseResp(0,"删除成功");
    }

    /**
     * 从订单页面发起支付
     * @param id
     * @param request
     * @return
     */
    @Override
    public String addArticleOrderFromOrder(Integer id, HttpServletRequest request) {
        //获取用户id
        Integer uid= new Integer(request.getAttribute("id").toString());
        List<ArticleOrder> byUserId = articleOrderRepository.findByUserId(uid);
        ArticleOrder articleOrder = new ArticleOrder();
        for(ArticleOrder artOrder :byUserId){
            if(artOrder.getArticleId()==id){
                articleOrder=artOrder;
            }
        }
        Double price = articleOrder.getPrice();
        articleOrder.setCreateTime(new Date());
        UUID uuid = UUID.randomUUID();
        articleOrder.setOrderNum(uuid.toString());
        articleOrderRepository.saveAndFlush(articleOrder);
        Map map = new HashMap();
        map.put("orderId",uuid.toString());
        map.put("price",price);
        return ArticlePayUtil.pay(map);
    }
}
